# -*- coding: utf-8 -*-

"""
@author: Laowang
@contact: QQ:1125564921
@Created on: 2022/6/20
@Remark: 
"""

from sqlalchemy.orm import Session


from framework_strategy.gopy.dao.orderline import StrategyModel, StrategyOrderLine, BaseModel
from framework_strategy.gopy.trader.engine import MainEngine, EventEngine, BaseEngine, Event
from framework_strategy.gopy.base.constant import EVENT_DB_DIRVER_PERSIST, EVENT_NOSQL_DIRVER_CACHE
from .core import create_engine


# direct
class DriverEngine(BaseEngine):
    """
    数据驱动引擎：
        描述：
        1. 通过推送事件来驱动数据存储
        2. 通过指定推送完成数据存储 
        参数：
        1. 数据存储默认选择MySQL
        2. 数据缓存选择Redis
    """
    _interval = 3

    def __init__(self, main_engine: MainEngine, event_engine: EventEngine):

        engine_name = "driver"
        super().__init__(main_engine, event_engine, engine_name)
        _, self._session: Session = create_engine()
        self._cache = object

    def persist(self, model: BaseModel):
        # event change model
        # TODO
        # self._session.add(model)
        pass

    def cache(self, model: BaseModel):
        # event change model
        # TODO 等待开放
        # self._cache.set(format(model))
        pass

    def on_event(self):
        """ """
        self.event_engine.on_bind(EVENT_DB_DIRVER_PERSIST, self.persist)
        self.event_engine.on_bind(EVENT_NOSQL_DIRVER_CACHE, self.cache)


